//
//  UIButton+Extension.swift
//  News
//
//  Created by panjiulong on 2017/7/21.
//  Copyright © 2017年 panjiulong. All rights reserved.
//

import UIKit

extension UIButton{
    convenience init(title:String,titleColor:UIColor, selectedColor:UIColor?, titleFont:UIFont,target:Any?,action:Selector){
        self.init(type: .custom)
        self.setTitle(title, for: .normal)
        titleLabel?.font = titleFont
        self.setTitleColor(titleColor, for: .normal)
        self.setTitleColor(selectedColor, for: .selected)
        self.addTarget(target, action: action, for: .touchUpInside)

    }
    convenience init(title:String,titleColor:UIColor, selectedColor:UIColor, titleFont:UIFont,backgroundImage:String,cornerRadius:CGFloat,target:Any?,action:Selector){
        self.init(title: title, titleColor: titleColor, selectedColor: selectedColor, titleFont: titleFont, target: target, action: action)
        self.setBackgroundImage(UIImage(named:backgroundImage), for: .normal)
        self.layer.cornerRadius = cornerRadius
        self.layer.masksToBounds = true
    }
    convenience init(image:String,highlightImage:String,target:Any?,action:Selector){
        self.init(type: .custom)
        self.setBackgroundImage(UIImage(named:image), for: .normal)
        self.setBackgroundImage(UIImage(named:highlightImage), for: .highlighted)
        self.addTarget(target, action: action, for: .touchUpInside)
    }
    convenience init(image:String,selectedImage:String,target:Any?,action:Selector){
        self.init(type: .custom)
        self.setBackgroundImage(UIImage(named:image), for: .normal)
        self.setBackgroundImage(UIImage(named:selectedImage), for: .selected)
        self.addTarget(target, action: action, for: .touchUpInside)
    }
    convenience init(title:String,titleColor:UIColor, selectedColor:UIColor, titleFont:UIFont,borderColor:UIColor,borderWidth:CGFloat,cornerRadius:CGFloat,target:Any?,action:Selector){
    
        self.init(title: title, titleColor: titleColor, selectedColor: selectedColor, titleFont: titleFont, target: target, action: action)
        self.layer.cornerRadius = cornerRadius
        self.layer.borderColor = borderColor.cgColor
        self.layer.borderWidth = borderWidth
        self.layer.masksToBounds = true
    }
    convenience init(title:String,titleColor:UIColor, selectedColor:UIColor?, titleFont:UIFont, backgroundColor:UIColor,cornerRadius:CGFloat,target:Any?,action:Selector){
        
        self.init(title: title, titleColor: titleColor, selectedColor: selectedColor, titleFont: titleFont, target: target, action: action)
        self.backgroundColor = backgroundColor
        self.layer.cornerRadius = cornerRadius
        self.layer.masksToBounds = true
    }
    
    func showProgress(){
        self.isUserInteractionEnabled = false
        self.titleLabel?.alpha = 0.0
        let progress = UIImageView.init(image: UIImage.init(named: "loading_light"))
        progress.frame = CGRect.init(x: 0, y: 0, width: 20, height: 20)
        progress.center = CGPoint.init(x: self.bounds.size.width/2, y: self.bounds.size.height/2)
        progress.tag = 999
        self.addSubview(progress)
        let animation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        animation.fromValue = NSNumber.init(value: 0.0)
        animation.toValue = NSNumber.init(value: Double.pi * 2)
        animation.duration = 2
        animation.autoreverses = false
        animation.fillMode = CAMediaTimingFillMode.forwards
        animation.repeatCount = MAXFLOAT
        progress.layer.add(animation, forKey: nil)
    }
    
    func hiddenProgress(){
        self.isUserInteractionEnabled = true
        self.titleLabel?.alpha = 1.0
        let progress = self.viewWithTag(999)
        progress?.removeFromSuperview()
    }
    
}

